* Put together the sample dataset, 1983-2021

* Variables needed
* ID
* Year
* Design weight
* Age
* Gender 
* District 
* Education 
* Vote choice

* 1983-2017 data
import spss using "sampdata/0.ICENES_1983_til_2017_islenska_LOKAGOGN_1utg.sav", clear

* Id and year
rename User_Id pid 
rename ELECYEAR year

* 1987 weight	
gen xswt=1
	replace xswt=weight if weight==1.64

* From fresh sample?
gen fresh=1 
 replace fresh=0 if sample87==1	
	
* Age
recode age (999=.) 
gen agecat=.
	replace agecat=1 if inrange(age,18,29)
	replace agecat=2 if inrange(age,30,49)
	replace agecat=3 if inrange(age,50,64)
	replace agecat=4 if inrange(age,65,150)

* Gender	
recode gender (1=1) (2=0) (9=.), gen(male)

* District	
gen district=. 
	decode elecdist_m, gen(kjördæmi)
	replace district=1 if kjördæmi=="Norðausturkjördæmi"
	replace district=2 if kjördæmi=="Norðvesturkjördæmi"
	replace district=3 if kjördæmi=="Reykjavíkurkjördæmi norður"
	replace district=4 if kjördæmi=="Reykjavíkurkjördæmi suður"
	replace district=5 if kjördæmi=="Suðurkjördæmi"
	replace district=6 if kjördæmi=="Suðvesturkjördæmi"
	replace district=11 if kjördæmi=="Reykjavíkurkjördæmi"
	replace district=12 if kjördæmi=="Reykjaneskjördæmi"
	replace district=13 if kjördæmi=="Vesturlandskjördæmi"
	replace district=14 if kjördæmi=="Vestfjarðarkjördæmi"
	replace district=15 if kjördæmi=="Norðurlandskjördæmi vestra"
	replace district=16 if kjördæmi=="Norðurlandskjördæmi eystra"
	replace district=17 if kjördæmi=="Austurlandskjördæmi"
	replace district=18 if kjördæmi=="Suðurlandskjördæmi"
	
	
* Education	
	* Fix 2013 data
	tostring pid, gen(id) format("%15.0f")
	gen User_Idsub=substr(id,9,100)

	preserve
	usespss "sampdata\icenes_2013_opin_adgangur_islenska_1utg.sav", clear
	recode education (2/4=1) (5 7 = 2) (6=3) (8=4) (90/max=.), gen(educ)
	tostring User_Id, gen(id) format("%15.0f")
	gen User_Idsub=substr(id,9,100)
	keep User_Idsub educ
	tempfile t
	save `t'
	restore
	
	merge n:1 User_Idsub using `t'
	replace education4=4 if educ==4 & year==2013
	drop educ 
	
	* Distribute still in school to highest degree ish
	replace education4 = 1 if education4==6 & age<21
	replace education4 = 2 if education4==6 & inrange(age,21,105)
	
	recode education4 (1=1) (2 3=2) (4=3) (nonm=.), gen(educ)
	
* Vote choice 
	clonevar party=prtvote_m_flo 
		replace party=89 if party==7 & year==1987
		replace party=89 if party==5 & year==1999
		replace party=89 if party==5 & year==2009
		replace party=89 if party==11 & year==2017
		recode party (94 96 97 98 99 = .)
		replace party=0 if vote_m==2
		recode party (8=10) (9=12) (10=11) (11=39) (12=40) (13=50) (14=52) (15=51)
		
	keeporder pid year fresh xswt male agecat district educ party 

frame create i21 
frame change i21 
		
	* 2021 data
	import spss using "sampdata/iskos_i_opinn_20221018.sav", clear

	rename User_Id pid 	
	gen year=2021
	gen xswt=1
	
	* From fresh sample
	gen fresh=1 
	replace fresh=0 if mode==3	
 
	* Age
	gen age=2021-yob
	gen agecat=.
		replace agecat=1 if inrange(age,18,29)
		replace agecat=2 if inrange(age,30,49)
		replace agecat=3 if inrange(age,50,64)
		replace agecat=4 if inrange(age,65,150)

	* Gender	
	recode gender (1=1) (2=0) (9=.), gen(male)

	* District	
	gen district=. 
		decode elecdist, gen(kjördæmi)
		replace district=1 if kjördæmi=="Norðausturkjördæmi"
		replace district=2 if kjördæmi=="Norðvesturkjördæmi"
		replace district=3 if kjördæmi=="Reykjavíkurkjördæmi norður"
		replace district=4 if kjördæmi=="Reykjavíkurkjördæmi suður"
		replace district=5 if kjördæmi=="Suðurkjördæmi"
		replace district=6 if kjördæmi=="Suðvesturkjördæmi"

	* Education 	
	recode educationflo (10 20 =1) (30 40=2) (50 60 70 80 90=3) (nonm=.), gen(educ)

	* Vote
	recode prtvote21 (6=40) (8=50) (10=52) (11=51) (12=89) (13=89) (14=89) (990=89) (992 993 994 997 998 999=.), gen(party)

	keeporder pid year fresh xswt male agecat district educ party 

frame change default 
frameappend i21	

	label variable fresh "Is R from fresh sample?"
	label variable agecat "Age category"
	label define agecat 1 "18-29" 2 "30-49" 3 "50-64" 4 "65+"
	label values agecat agecat 

	label variable male "Is R male?"
	label define male 1 "Male" 0 "Female/other"
	label values male male

	label variable educ "Education"
	label define educ 1 "Primary or less" 2 "Secondary" 3 "Tertiary"
	label values educ educ

	do "temp/districts"	
	label values district districts

	do "temp/parties"
	label values party parties

save "temp\sampledata.dta", replace
	
	
	
	
	
 